package kr.ac.hanyang.cs.jhpark;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class SelectDialog extends ListActivity {
	public static final int SELECT_APP				= 1;
	public static final int SELECT_ADDR				= 1;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    // TODO Auto-generated method stub
        PkgInfo pkgInfo								= new PkgInfo(this);
        pkgInfo.setDebug(true);
        ArrayList<PkgInfo> pkgList					= pkgInfo.getPackageInfos();

	    //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings));
	    //getListView().setTextFilterEnabled(true);

        PkgAdapter m_adapter						= new PkgAdapter(this, R.layout.itemrow_pkginfo, pkgList);
        setListAdapter(m_adapter);
        
	}

 
    private class PkgAdapter extends ArrayAdapter<PkgInfo> {
    	private ArrayList<PkgInfo> items;
    	
    	public PkgAdapter(Context context, int textViewResourceId, ArrayList<PkgInfo> items) {
    		super(context, textViewResourceId, items);
    		this.items								= items;
    	}

    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v									= convertView;

    		if (v == null) {
    			LayoutInflater vi					= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    			v									= vi.inflate(R.layout.itemrow_pkginfo, null);
            }


    		PkgInfo pkg								= items.get(position);

    		if (pkg != null) {
    			TextView tv							= (TextView) v.findViewById(R.id.dlg_AppName);
    			ImageView iv						= (ImageView)v.findViewById(R.id.dlg_Icon); 
    			Button selBtn						= (Button)   v.findViewById(R.id.dlg_BtnSelect);

    			if (tv != null)
    				tv.setText(pkg.getAppname());

    			if (iv != null)
    				iv.setImageDrawable(pkg.getIcon());
    			
    			// 현재 item의 position을 버튼에 tagging한 후, 클릭 시 이벤트 리스터에 넘김
    			selBtn.setTag(position);
    			selBtn.setOnClickListener(btnClickListner);

            }

            return v;
    	}
    	
    	private Button.OnClickListener btnClickListner = new Button.OnClickListener() {
    		public void onClick(View v) {
    			// tagging되어 있는 position을 읽어온 후, 해당 App 정보를 가져와서 반환
    			PkgInfo selectedApp					= items.get((Integer) v.getTag());

    			// 호출한 intent를 얻어와, 값을 넣고 반환
    			Intent intent						= getIntent();
    			intent.putExtra("Appname", selectedApp.getAppname());
    			intent.putExtra("Package", selectedApp.getPname());
    			setResult(RESULT_OK, intent);
    			finish();
    		}
    		
    	};

    }
    



}
